/**
 * 
 */
package com.whf15.o2o.web.shopadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whf15.o2o.dto.ProductCategoryExecution;
import com.whf15.o2o.dto.Result;
import com.whf15.o2o.entity.ProductCategory;
import com.whf15.o2o.entity.Shop;
import com.whf15.o2o.enums.ProductCategoryStateEnum;
import com.whf15.o2o.exceptions.ProductCategoryOperationException;
import com.whf15.o2o.service.ProductCategoryService;

/** 
* @author 作者 :whfo 
* @version 创建时间：2021年6月29日 下午3:51:38 
* 类说明 
*/
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@RequestMapping(value="/getproductcategorylist",method=RequestMethod.GET)
	@ResponseBody
	private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
		//To be removed
//		Shop shop = new Shop();
//		shop.setShopId(3L);
//		request.getSession().setAttribute("currentShop", shop);
		
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
		List<ProductCategory> list = null;
		if(currentShop != null && currentShop.getShopId() > 0) {
			list = productCategoryService.getProductCategoryList(currentShop.getShopId());
			return new Result<List<ProductCategory>>(true,list);
		}else {
			ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
			return new Result<List<ProductCategory>>(false,ps.getStateInfo(),ps.getState());
		}
	}
	
	@RequestMapping(value="/addproductcategories",method=RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> addProductCategories(
			@RequestBody List<ProductCategory> productCategories,
			HttpServletRequest request){
		
		Map<String,Object> modelMap = new HashMap<String,Object>();
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
		//获取前端传来的数据后，遍历
		for(ProductCategory pc : productCategories) {
			pc.setShopId(currentShop.getShopId());
		}
		//控制
		if(productCategories != null && productCategories.size() > 0 ) {
			try {
				ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategories);
				if(pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}
			} catch (RuntimeException e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
				return modelMap;
			}
			
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "最少你都输入一个挂");
		}
		return modelMap;
	}
	@RequestMapping(value="/removeproductcategory",method=RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> removeProductCategory(Long productCategoryId,
			HttpServletRequest request){
			
		Map<String,Object> modelMap = new HashMap<String,Object>();
		
		if(productCategoryId != null && productCategoryId > 0) {
			try {
				Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
				ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
				if(pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}
			}catch (ProductCategoryOperationException e) {
					modelMap.put("success", false);
					modelMap.put("errMsg", e.toString());
					return modelMap;
				}
		}else {
			modelMap.put("success", false);
			//至少删除一个商品类别
			modelMap.put("errMsg", "你想删边个咯？");
		}
		
		return modelMap;
		
	}

}
